using System;
using CodeCampServerLite.UI;
using CodeCampServerLite.UI.Controllers;
using CodeCampServerLite.UI.Models;
using MvcContrib.TestHelper.Ui;
using MvcContrib.TestHelper.WatiN;
using NUnit.Framework;
using WatiN.Core;
using Should.Extensions.AssertExtensions;

namespace UITests
{
	public class UiTestBase
	{
		private static readonly InputTesterFactoryRegistry _factoryRegistry 
			= new InputTesterFactoryRegistry();

		[SetUp]
		public void FixtureSetup()
		{
			var baseUrl = "http://localhost:8084";
			Browser = new WatinDriver(new IE(baseUrl), baseUrl);
		}

		[TearDown]
		public void TearDown()
		{
			Browser.Dispose();
			Browser = null;
		}


		protected IBrowserDriver Browser { get; private set; }

		protected virtual void NavigateLink(string rel)
		{
			Browser.ClickLink(rel);
		}

		protected InputForm<T> Form<T>()
		{
			return new InputForm<T>(Browser, _factoryRegistry);
		}
	}

	[TestFixture]
	public class EditingConferenceTestsWithMvcContrib : UiTestBase
	{
		[Test]
		public void Should_be_able_to_edit_conference()
		{
			NavigateLink(WebSiteNav.Conferences);

			NavigateLink("Edit CodeMash");

			Form<ConferenceEditModel>()
				.Input(m => m.Name, "CodeMashFoo")
				.Submit();

			Browser.VerifyPage<ConferenceController>(c => c.Index());

			Browser.AssertValue<ConferenceListModel[]>(
				c => c[0].Name, 
				"CodeMashFoo");
		}
	}
}